package edu.szu.huhanpeng.doodlingFight.controller.showController.gameStart 
{
	import com.aka.tools.funcTools.CountToCall;
	import com.bit101.components.Panel;
	import com.bit101.components.Window;
	import com.greensock.TweenLite;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.globalConst.Setting;
	import edu.szu.huhanpeng.doodlingFight.model.data.GameData;
	import edu.szu.huhanpeng.doodlingFight.model.GameProxy;
	import edu.szu.huhanpeng.doodlingFight.view.components.PlayerWindow;
	import edu.szu.huhanpeng.doodlingFight.view.GameWindowMed;
	import edu.szu.huhanpeng.doodlingFight.view.MsgWindowMed;
	import edu.szu.huhanpeng.doodlingFight.view.PlayerWindowMed;
	import edu.szu.huhanpeng.doodlingFight.view.StagePanelMed;
	import flash.events.Event;
	import org.puremvc.as3.interfaces.IAsyncCommand;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.AsyncCommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class InitPlayWindowsSc extends AsyncCommand implements IAsyncCommand, INotifier 
	{
		private static const SETTING:Setting = GlobalConst.setting;
		
		private var _stagePanel:Panel;
		
		override public function execute(note:INotification):void 
		{
			_stagePanel = facade.retrieveMediator( StagePanelMed.NAME ).getViewComponent() as Panel;
			CountToCall.add( commandComplete, 4, null, true );
			initGameWindow();
			initPlayersWindow();
			initMsgWindow();
		}
		
		private function initGameWindow():void 
		{
			var window:Window = new Window( _stagePanel, SETTING.GAME_WINDOW_X, SETTING.GAME_WINDOW_Y, SETTING.GAME_WINDOW_NAME );
			window.setSize( SETTING.GAME_WINDOW_WIDTH, SETTING.GAME_WINDOW_HEIGHT );
			setWindow( window );
			facade.registerMediator( new GameWindowMed(window) );
		}
		
		private function initPlayersWindow():void 
		{
			var gameData:GameData = facade.retrieveProxy( GameProxy.NAME ).getData() as GameData
			//player1
			var window:PlayerWindow = new PlayerWindow( _stagePanel, SETTING.P_ONE_WINDOW_X, SETTING.P_ONE_WINDOW_Y, SETTING.P_ONE_WINDOW_NAME );
			window.setSize( SETTING.PLAYER_WINDOW_WIDTH, SETTING.PLAYER_WINDOW_HEIGHT );
			setWindow( window );
			window.setPlayerName( gameData.pOneName );
			var med:PlayerWindowMed = new PlayerWindowMed(PlayerWindowMed.P_ONE_NAME, window);
			med.light();
			facade.registerMediator( med );
			
			//player2
			window = new PlayerWindow( _stagePanel, SETTING.P_TWO_WINDOW_X, SETTING.P_TWO_WINDOW_Y, SETTING.P_TWO_WINDOW_NAME );
			window.setSize( SETTING.PLAYER_WINDOW_WIDTH, SETTING.PLAYER_WINDOW_HEIGHT );
			setWindow( window );	
			window.setPlayerName( gameData.pTwoName );
			med = new PlayerWindowMed(PlayerWindowMed.P_TWO_NAME, window);
			med.dark();
			facade.registerMediator( med );
			
			
		}
		
		private function initMsgWindow():void 
		{
			var window:Window = new Window( _stagePanel, SETTING.MSG_WINDOW_X, SETTING.MSG_WINDOW_Y, SETTING.MSG_WINDOW_NAME );
			window.setSize( SETTING.MSG_WINDOW_WIDTH, SETTING.MSG_WINDOW_HEIGHT );
			setWindow( window );
			facade.registerMediator( new MsgWindowMed(window) )
		}
		
		private function setWindow(window:Window):void 
		{
			window.hasMinimizeButton = true;
			window.hasCloseButton = true;
			window.addEventListener( Event.CLOSE, onCloseClick );
			
			window.alpha = 0;
			TweenLite.to( window, SETTING.FADEIN, { alpha:1, onComplete:tweenComplete } );
		}
		
		private function tweenComplete():void 
		{
			CountToCall.count( commandComplete );
		}
		
		
		private function onCloseClick(e:Event):void 
		{
			var target:Window = e.currentTarget as Window;
			
			switch ( target.title ) 
			{
				case SETTING.GAME_WINDOW_NAME:
					target.x = SETTING.GAME_WINDOW_X;
					target.y = SETTING.GAME_WINDOW_Y;
				break;
				
				case SETTING.P_ONE_WINDOW_NAME:
					target.x = SETTING.P_ONE_WINDOW_X;
					target.y = SETTING.P_ONE_WINDOW_Y;
					break;
					
				case SETTING.P_TWO_WINDOW_NAME:
					target.x = SETTING.P_TWO_WINDOW_X;
					target.y = SETTING.P_TWO_WINDOW_Y;
					break;
					
				case SETTING.MSG_WINDOW_NAME:
					target.x = SETTING.MSG_WINDOW_X;
					target.y = SETTING.MSG_WINDOW_Y;
				default:
			}
		}
	}

}